package util

import (
	"log"
	"net"
	"time"
)

func CopyBuf(dst net.Conn, src net.Conn, readTimeout time.Duration, writeTimeout time.Duration) (written int64, err error) {
	buf := make([]byte, 32*1024)
	for {
		err = src.SetReadDeadline(time.Now().Add(readTimeout))
		if err != nil {
			return 0, err
		}
		n, err := src.Read(buf)
		if n > 0 {
			log.Default().Output(2, string(buf[:min(n, 256)]))
			err = dst.SetWriteDeadline(time.Now().Add(writeTimeout))
			if err != nil {
				return 0, err
			}
			write, err := dst.Write(buf[:n])
			if err != nil {
				//log.Println(err)
				return written, err
			}
			written += int64(write)
		}
		if err != nil {
			//log.Println(err)
			return written, err
		}

	}
}
